
package com.yimixian.app.wxapi;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelpay.PayResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.yimixian.app.common.Constants;
import com.yimixian.app.data.DataManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private DataManager mDataManager;
    private IWXAPI mWxApi;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setBackgroundDrawable(null);
        mDataManager = DataManager.getInstance();
        mWxApi = WXAPIFactory.createWXAPI(this, Constants.WECHAT_APP_ID);
        mWxApi.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        mWxApi.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp response) {
        if (response.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            PayResp payResponse = (PayResp) response;
            mDataManager.put(payResponse.prepayId, response.errCode);
        }

        finish();
    }
}
